<!--
 * @Description: 系统通知配置
-->
<template>
  <BaseMainSlot>
    <template v-slot:btnsBar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :page-no="94"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:searchbox>
      <SearchForm
        :formConfig="formConfig"
        :formData="searchlist"
        @search="queryPageList"
      ></SearchForm>
    </template>
    <template v-slot:content>
      <e-table
        class="right"
        ref="tableData"
        rowKey="noticeConfigNo"

        :table-data="tableData"
        :table-columns="tableLabel"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        :table-option="tableOption"
        @sizeChange="changePageSize"
        @pageChange="changeCurrentPage"
        @handleSelectionChange="handleSelectionChange"
        @tableUpdatePlus="mixTableUpdatePlus"
      >
        <template
          v-if="scope.row.noticeType !== undefined"
          slot="noticeType"
          slot-scope="scope"
        >
          <span v-if="scope.row.noticeType">必须送达</span>
          <span v-if="!scope.row.noticeType">允许丢失</span>
        </template>
        <template
          slot="operate"
          slot-scope="scope"
          v-if="scope.row.noticeConfigName"
        >
          <span class="table-btns-box" @click="handleEdit(scope.row,true)">
            <i class="el-icon-view"></i>
            <span>查看</span>
          </span>
          <span
            v-if="judgePermission(94, 'edit')"
            class="table-btns-box"
            @click="edit(scope.row)"
          >
            <i class="el-icon-edit-outline"></i>
            <span>修改</span>
          </span>
          <span
            v-if="judgePermission(94, 'delete')"
            class="table-btns-box table_del_btn"
            @click="handleDelete(scope.row.noticeConfigNo)"
          >
            <i class="el-icon-delete"></i>
            <span>删除</span>
          </span>
        </template>
      </e-table>
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="titleText + '系统通知信息'"
        :visible.sync="showAddEditDialog"
        :before-close="cancel"
        width="40%"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item label="通知名称:" class="w100p" prop="noticeConfigName">
            <el-input
              v-model="formData.noticeConfigName"
              clearable
              :disabled="disabled"
            />
          </el-form-item>
          <el-form-item
            label="通知人:"
            class="w100p"
            :required="true"
            :disabled="disabled"
          >
            <div class="tifier-add" v-if="!disabled">
              <el-button type="text" size="medium" @click="tifierAdd">
                <i class="el-icon-circle-plus"></i> 增加
              </el-button>
            </div>
            <div
              v-for="(item, index) in NotifierList"
              :key="index"
              class="w100p phone"
            >
              <el-select
                v-model="item.personNo"
                class="w50p"
                clearable
                filterable
                :disabled="disabled"
                @change="personData(item, index)"
              >
                <el-option
                  v-for="items in StaffData"
                  :key="items.personNo"
                  :label="items.contactsName"
                  :value="items.personNo"
                />
              </el-select>
              <div class="w50p">
                <el-radio
                  v-model="item.radio"
                  :label="1"
                  @change="tifierChange(item.radio, index)"
                  :disabled="disabled"
                >全选</el-radio>
                <el-radio
                  v-model="item.radio"
                  :label="2"
                  @change="tifierChange(item.radio, index)"
                  :disabled="disabled"
                >全不选</el-radio>
                <el-button
                  class="text-btn-cursor cursor-delete"
                  type="text"
                  size="small"
                  :disabled="disabled"
                  @click="tifierDelete(item, index)"
                >
                  <i class="el-icon-delete"></i> 删除
                </el-button>
              </div>
              <div class="w100p">
                <el-checkbox
                  v-model="item.cidFlag"
                  size="medium"
                  :disabled="disabled"
                >APP</el-checkbox>
                <!-- <el-checkbox
                  v-model="item.weChatFlag"
                  size="medium"
                >微信</el-checkbox>-->
                <el-checkbox
                  v-model="item.phoneFlag"
                  size="medium"
                  :disabled="disabled"
                >手机</el-checkbox>
                <el-checkbox
                  v-model="item.emailFlag"
                  size="medium"
                  :disabled="disabled"
                >邮箱</el-checkbox>
              </div>
            </div>
          </el-form-item>
                    <el-form-item label="通知类型:" class="w100p">
                      <el-radio
              v-model="formData.noticeType"
              :label="true"
            >必须送达</el-radio>
            <el-radio
              v-model="formData.noticeType"
              :label="false"
            >允许丢失</el-radio>
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">取 消</el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >保 存</el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import SearchForm from "@/components/searchForm";
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import nowpagereq from "@/api/monitoring-alarm/system-notification-configuration";
import nowalarm from "@/api/monitoring-alarm/alarm-contact-management";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";
export default {
  name: "SystemNotificationConfiguration",
  components: { SearchForm, ActionBar, Pagination, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      formConfig: [
        {
          label: "通知名称",
          prop: "noticeConfigName",
          type: "input",
        },
        {
          label: "联系人",
          prop: "personNo",
          type: "select",
          options: [], // StaffData
          optionKey: { label: "contactsName", value: "personNo" },
        },
        {
          label: "通知方式",
          prop: "noticeWay",
          type: "input",
        },
        // {
        //   label: "通知类型",
        //   prop: "noticeType",
        //   type: "radio",
        //   radios: [
        //     { label: "必须送达", value: true },
        //     { label: "允许丢失", value: false },
        //   ],
        // },
      ],
      rules: {
        noticeConfigName: [
          { required: true, message: "请填写姓名", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请填写手机号", trigger: "blur" },
          { type: "number", message: "手机号为数字" },
        ],
      },
      // 搜索内容
      searchlist: {},
      // 选中的数据项数组 alarmContactId组成 (此数组最多为一个)
      selectIdList: [],
      NotifierList: [
        {
          userName: "",
          personNo: "",
          phoneFlag: false,
          weChatFlag: false,
          emailFlag: false,
          cidFlag: false,
          radio: 3,
        },
      ],
      StaffData: [],
      dialogTitle: "",
      showAddEditDialog: false,
      disabled: false,
      // 表单展示项
      tableLabel: [
        {
          label: "通知名称",
          param: "noticeConfigName",
          show: true,
        },
        {
          label: "通知类型",
          param: "noticeType",
          show: true,
          slotName: "noticeType",
        },
      ],
      // 表单事件
      tableOption: {
        label: "操作",
        width: 220,
        options: [
          {
            label: "操作",
            slotName: "operate",
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {
    nowChanceStau: {
      handler(val) {
        if (val === "") {
          this.NotifierList = [
            {
              userName: "",
              personNo: "",
              phoneFlag: false,
              weChatFlag: false,
              emailFlag: false,
              cidFlag: false,
              radio: 3,
            },
          ];
        } else if (val === "edit") {
          this.NotifierList = this.formData.noticeMethodList;
        }
      },
    },
  },
  created() {
    // 获取表格数据
    this.queryPageList();
    this.queryDataList();
  },
  mounted() {},
  methods: {
    queryPageList(e) {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        sortList: this.sortList || [],
        entity: {
          ...this.searchlist,
          ...e,
          organizationNo: sessionStorage.organizationNo,
        },
      };

      nowpagereq.getlist(params).then((res) => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error("查询失败");
        }
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
        this.addblankmet();
        this.resetActionBar();
      });
    },
    async queryDataList() {
      // 员工
      const { data: Staff } = await nowalarm.querybycondition();
      this.StaffData = Staff;
      this.formConfig[1].options = Staff;
    },
    // // 添加
    // add() {
    //   this.openDialog("add");
    //   this.formData = {};
    // },
    // // 取消
    cancel() {
      this.closeDialog();
      this.formData = {};
    },
    personData(val, index) {
      this.StaffData.forEach((item) => {
        if (val.personNo === item.personNo) {
          console.log(val);
          this.$set(this.NotifierList, index, {
            ...val,
            userName: item.contactsName,
          });
          console.log("personData", this.NotifierList);
        }
      });
    },
    tifierChange(item, index) {
      let notifyData = this.NotifierList[index];
      if (item === 1) {
        (notifyData.cidFlag = true),
          (notifyData.weChatFlag = true),
          (notifyData.phoneFlag = true),
          (notifyData.emailFlag = true),
          (notifyData.radio = 1),
          this.$set(this.NotifierList, index, notifyData);
      } else if (item === 2) {
        (notifyData.cidFlag = false),
          (notifyData.weChatFlag = false),
          (notifyData.phoneFlag = false),
          (notifyData.emailFlag = false),
          (notifyData.radio = 2),
          this.$set(this.NotifierList, index, notifyData);
      } else {
        this.$set(this.NotifierList, index, {
          radio: 3,
        });
      }
    },
    tifierDelete(item, index) {
      this.NotifierList.splice(index, 1);
    },
    tifierAdd() {
      console.log(this.NotifierList, this.StaffData);
      this.NotifierList.forEach((el) => {
        this.StaffData = this.StaffData.filter(
          (ell) => el.personNo !== ell.personNo
        );
      });
      this.NotifierList.push({
        userName: "",
        personNo: "",
        phoneFlag: false,
        weChatFlag: false,
        emailFlag: false,
        cidFlag: false,
        radio: 3,
      });
    },
    async handleEdit(row, disabled = false) {
      console.log("handleEdit", disabled);
      this.disabled = disabled;
      if (disabled) {
        this.dialogTitle = "";
      }
      this.showAddEditDialog = true;
      const { data: rs } = await nowpagereq.queryByNo(row.noticeConfigNo);
      this.formData = JSON.parse(JSON.stringify(rs));
      this.NotifierList = rs.noticeMethodList;
    },
    handleSelectionChange(list) {
      this.selectIdList = [];
      let notvalue = true;
      this.selectIdList = list.map((item) => {
        if (item.noticeConfigNo) {
          return item.noticeConfigNo;
        } else {
          notvalue = false;
          return "";
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
        return;
      }
      if (this.selectIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      nowpagereq.dellist(this.selectIdList).then((res) => {
        if (res.code === 1) {
          this.$message.success("删除成功");
          this.queryPageList();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    handleDelete(noticeConfigNo) {
      // nowpagereq.del(noticeConfigNo).then((res) => {
      //   if (res.code === 1) {
      //     this.$message.success("删除成功");
      //     this.queryPageList();
      //   } else {
      //     this.$message.error("删除失败");
      //   }
      // });
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass",
      })
        .then(() => {
          nowpagereq.del(noticeConfigNo).then((res) => {
            if (res.code === 1) {
              this.$message.success("删除成功");
              this.queryPageList();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          //
          this.$message("取消删除");
        });
    },
    save() {
      this.formData.noticeMethodList = this.NotifierList;
      console.log("save", this.formData);
      if (this.nowChanceStau === "edit") {
        // 编辑接口
        // this.formData.routePointMapperList = this.tagsList;
        nowpagereq.editlist(this.formData).then((res) => {
          if (res.code === 1) {
            this.$message.success("修改成功！");
            this.closeDialog();
            this.queryPageList();
          } else {
            this.$message.error("修改失败！");
          }
        });
      } else if (this.nowChanceStau === "add") {
        if (this.NotifierList.length === 0) {
          return this.$message.warning("通知人不能为空");
        }
        if (this.NotifierList[0].personNo === "") {
          return this.$message.warning("通知人不能为空");
        }
        this.formData.organizationNo = sessionStorage.organizationNo;
        nowpagereq.addlist(this.formData).then((res) => {
          if (res.code === 1) {
            this.$message.success("新增成功！");
            this.closeDialog();
            this.queryPageList();
          } else {
            this.$message.error("新增失败！");
          }
        });
      }
    },
    openDialog(type) {
      this.showAddEditDialog = true;
      this.dialogTitle = type === "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.showAddEditDialog = false;
      this.dialogNoticeFormVisible = false;
      this.addActive = true;
      this.dialogTitle = null;
      this.nowChanceStau = "";
    },
  },
};
</script>

<style scoped lang="scss">
.phone {
  width: 100%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  margin: 5px;
  justify-content: space-between;
  .el-select {
    width: 48%;
  }
}
.tifier-add {
  position: absolute;
  left: -60px;
  top: 32px;
}
</style>
